<template>
  <div class="app-container">
    <el-dialog :title="pageTitle" :visible.sync="dialogFormVisible" :before-close="beforeClose">
      <el-form :model="form">
        <el-form-item label="分类名称：" :label-width="formLabelWidth">
          <el-input v-model="form.title" minlength="2" maxlength="10" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类英文名称：" :label-width="formLabelWidth">
          <el-input v-model="form.mark" placeholder="请输入分类英文名称" />
        </el-form-item>
        <el-form-item label="排序比重：" :label-width="formLabelWidth">
          <el-input v-model="form.sort" type="number" min="0" max="99" placeholder="请输入排序比重" />
        </el-form-item>
        <el-row>
          <el-col v-if="this.$store.getters.role.store_id == 0" :span="12">
            <el-form-item label="选择店铺：" :label-width="formLabelWidth">
              <el-select v-model="form.store_id" placeholder="请选择店铺">
                <el-option
                  v-for="item in shops"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类状态：" :label-width="formLabelWidth">
              <el-switch
                v-model="form.status"
                :active-text="form.status==1?'启用':'禁用'"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canel">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { handle, getStore } from '@/api/goodType'
export default {
  name: 'RoleHandle',
  data() {
    return {
      form: {
        id: 'new',
        store_id: '',
        title: '',
        mark: '',
        status: '1',
        sort: 0
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
      pageTitle: '添加商品分类',
      shops: []
    }
  },

  methods: {
    beforeClose() {
      this.canel()
    },

    add() {
      this.form.id = 'new'
      this.form.store_id = ''
      this.form.title = ''
      this.form.mark = ''
      this.form.status = '1'
      this.form.sort = 0
      this.pageTitle = '添加商品分类'
      this.dialogFormVisible = true
      getStore({}).then(res => {
        this.shops = res.data
      })
    },

    edit(item) {
      this.form.id = item.id
      this.form.store_id = item.store_id
      this.form.title = item.title
      this.form.status = item.status.toString()
      this.form.describe = item.describe
      this.form.menu_ids = item.menu_ids
      this.form.rule_ids = item.rule_ids
      this.pageTitle = '编辑商品分类'
      this.dialogFormVisible = true

      getStore({}).then(res => {
        this.shops = res.data
      })
    },

    canel() {
      this.dialogFormVisible = false
      this.form.id = 'new'
      this.form.title = ''
      this.form.describe = ''
      this.form.status = '1'
      this.form.menu_ids = ''
      this.form.rule_ids = ''
      this.nodes = []
    },

    sure() {
      handle(this.form).then(res => {
        Message({
          message: res.data.info,
          type: 'success',
          duration: 2000
        })
        this.$emit('afterSave')
        this.canel()
      })
    }
  }
}
</script>
